#!/usr/bin/env python
# encoding: utf-8
"""
binpacktest.py

Created by 黄 冬 on 2007-12-12.
Copyright (c) 2007 __MyCompanyName__. All rights reserved.
"""

import unittest
import logging.config

log = logging.getLogger('xbaytable.tests.binpacktest')
logging.basicConfig(level=logging.DEBUG)

from xbaytable.conf import *
from xbaytable.utils import binpack

class BinpackTest(unittest.TestCase):
    def setUp(self):
        pass

    def test_unpackhead(self):
        """测试对二进制的包头进行解包"""
        command,sequence_id = binpack.unpackhead('\x00\x01\x00\x01')
        self.assertEqual(command,TABLE_BIND)
        self.assertEqual(sequence_id,1)

    def test_genbody_table_bind(self):
        """测试生成二进制的body"""
        binstring = binpack.genbody(TABLE_BIND,'hd','1234567890abcdef',1,'1212121212')
        self.assertEqual(binstring,'hd\x00\x00\x00\x001234567890abcdef\x00\x011212121212')

    def test_genbody_table_bind_resp(self):
        """测试生成二进制的body"""
        binstring = binpack.genbody(TABLE_BIND_RESP,1,1)
        self.assertEqual(binstring,'\x00\x01\x00\x01')

    def test_genmsg_table_bind_msg(self):
        """测试生成二进制的msg"""
        msg = binpack.genmsg(TABLE_BIND,1,'hd','1234567890abcdef',1,'1212121212')
        self.assertEqual(msg,'\x00\x01\x00\x01hd\x00\x00\x00\x001234567890abcdef\x00\x011212121212')

    def test_unpack_table_bind(self):
        """测试解table_bind body"""
        user_name,authentiator_code,version,timestamp = binpack.unpackbody(TABLE_BIND,'hd\x00\x00\x00\x001234567890abcdef\x00\x011212121212')
        self.assertEqual(user_name.rstrip('\x00'),'hd')
        self.assertEqual(authentiator_code,'1234567890abcdef')
        self.assertEqual(version,1)
        self.assertEqual(timestamp,'1212121212')

    def test_genmsg_table_unbind(self):
        """测试生成无内容的二进制msg"""
        msg = binpack.genmsg(TABLE_UNBIND,1)
        self.assertEqual(msg,'\x00\x03\x00\x01')

    def test_genmsg_table_set(self):
        """测试生成动态长度的二进制table_set msg"""
        msg = binpack.genmsg(TABLE_SET,1,1,'a','abc')
        self.assertEqual(msg,'\x00\x07\x00\x01\x00\x01aabc')

    def test_unpack_table_set(self):
        """测试解动态的table_set body"""
        namelen,name,value=binpack.unpackbody(TABLE_SET,'\x00\x01aabc')
        self.assertEqual(namelen,1)
        self.assertEqual(name,'a')
        self.assertEqual(value,'abc')

    def test_genmsg_table_append(self):
        """生成动太长度的二进制table_append msg"""
        msg = binpack.genmsg(TABLE_APPEND,1,1,'a','abc')
        self.assertEqual(msg,'\x00\t\x00\x01\x00\x01aabc')

    def test_unpack_table_append(self):
        """测试解动态的table_append body"""
        namelen,name,value=binpack.unpackbody(TABLE_APPEND,'\x00\x01aabc')
        self.assertEqual(namelen,1)
        self.assertEqual(name,'a')
        self.assertEqual(value,'abc')

    def test_genmsg_table_del(self):
        """生成动态长度的二进制table_del msg"""
        msg = binpack.genmsg(TABLE_DEL,1,'a')
        self.assertEqual(msg,'\x00\x0b\x00\x01a')

    def test_unpack_table_del(self):
        """测试解动态的table_del body"""
        name=binpack.unpackbody(TABLE_DEL,'a')
        self.assertEqual(name,'a')

    def test_genmsg_table_del_resp(self):
        """生成动态长度的二进制table_del_resp msg"""
        msg = binpack.genmsg(TABLE_DEL_RESP,1,0,'a')
        self.assertEqual(msg,'\x00\x0c\x00\x01\x00\x00a')

    def test_unpack_table_del_resp(self):
        """测试解动态的table_del_resp body"""
        status,name=binpack.unpackbody(TABLE_DEL_RESP,'\x00\x00a')
        self.assertEqual(status,0)
        self.assertEqual(name,'a')

    def test_genmsg_table_get(self):
        """生成动态长度的二进制table_get msg"""
        msg = binpack.genmsg(TABLE_GET,1,'a')
        self.assertEqual(msg,'\x00\r\x00\x01a')

    def test_genmsg_table_get_resp(self):
        """生成动态长度的二进制table_get_resp msg"""
        msg = binpack.genmsg(TABLE_GET_RESP,1,0,1,'a','abcd')
        self.assertEqual(msg,'\x00\x0e\x00\x01\x00\x00\x00\x01aabcd')

    def test_unpack_table_get_resp(self):
        """解table_get_resp二进制包"""
        status,namelen,name,value=binpack.unpackbody(TABLE_GET_RESP,'\x00\x00\x00\x01aabcxiaohong')
        self.assertEqual(status,0)
        self.assertEqual(namelen,1)
        self.assertEqual(name,'a')
        self.assertEqual(value,'abcxiaohong')

def suite():
    """集合测试用例"""
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(BinpackTest, 'test'))
    return suite

if __name__ == '__main__':
    unittest.main()